package com.burgeon.backend.EnterpriseWeChat.rest.EWEmployee;
/**
 * 获取符合条件的导购 用于生成企业微信员工
 */

import com.burgeon.backend.Rest.base.RESTBase;
import org.json.JSONArray;
import org.springframework.util.MultiValueMap;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * 根据企业微信获取的userid即erp员工编号获取员工信息
 */
public class EWEmployeeIsInvite extends RESTBase {

    private String ip;
    private String account;
    private String password;

    public EWEmployeeIsInvite(String ip, String account, String password) {
        this.ip = ip;
        this.account = account;
        this.password = password;
    }

    /**
     * 发送并接收字符创
     *
     * @return
     */

    public String getData(String userid) throws Exception {
        String url = "http://" + ip + "/servlets/binserv/Rest";

        String str = RESULT(Data(userid), url);
        System.err.println(str);
        String code = new JSONArray(str).getJSONObject(0).getString("code");
        if (!"0".equals(code)) {
            throw new Exception();
        }

        JSONArray jsonArray = new JSONArray(str).getJSONObject(0).getJSONArray("rows");

        if (jsonArray.length()>0){

            return jsonArray.getJSONArray(0).getString(0);
        }

        return null;
    }

    private MultiValueMap Data(String userid) {
        HashMap params = new HashMap();
        ArrayList columns = new ArrayList();//查询字段


        columns.add("ISACTIVE");


        params.put("columns", columns);
        params.put("table", "C_V_EMPLOYEE");//表名

        params.put("count", "true");

//        HashMap paramss = new HashMap();
//        paramss.put("column", "NO");
//        System.err.println(userid);
//        paramss.put("condition",userid);
        HashMap paramss = new HashMap();//DOCNO  (AK)	单据编号
        HashMap expr1= new HashMap();
//        HashMap expr2= new HashMap();
        String ac = "'="+userid+"'";
        paramss.put("column", "'ID'");
        paramss.put("condition", ac);

//        String isActive = "'=Y'";
//        expr2.put("column", "'ISACTIVE'");
//        expr2.put("condition", isActive);
//        paramss.put("expr1",expr1);
//        paramss.put("expr2",expr2);
//        paramss.put("combine","and");

        params.put("params", paramss);

        return DATA("Query", params, account, password);
    }

    public static void main(String[] args) {
        EWEmployeeIsInvite g = new EWEmployeeIsInvite("39.104.73.78:5321", "nea@burgeon.com.cn", "burgeon5321");
        try {
            System.err.println(g.getData("747"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
